# -*- coding: utf-8 -*-
import scrapy
from myproject.items import BaiduItem

from urllib import parse
import time
from scrapy.http import Request
from scrapy.loader import ItemLoader

class BaiduSpider(scrapy.Spider):
    name = 'baidu'
    allowed_domains = ['tieba.baidu.com']
    start_urls = ['http://tieba.baidu.com/f/index/forumpark?pcn=%E9%AB%98%E7%AD%89%E9%99%A2%E6%A0%A1&pci=0&ct=1/']

    def parse(self, response):
        lis = response.css('.ba_list .ba_info')
        if(lis):
           for li in lis:
                item = BaiduItem()
                item['create_time']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                item['title'] = li.css('.ba_content .ba_name::text').extract_first()
                item['url'] = (li.css('.ba_href::attr(href)').extract_first())
                item['url'] = parse.unquote(parse.urljoin(response.url, item['url']))
                item['fav_nums'] = '213'
                yield item
        next_url = response.css('.pagination  .next::attr(href)').extract_first()
        print("next_url`````````````````" + next_url)
        last_next_url=''
        if next_url:
           last_next_url = url = parse.urljoin(response.url, next_url)
           yield Request(url=last_next_url, callback=self.parse)
